home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / Stopwatch2.3 / Source / SessionEditor.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  164 lines

  1. /*
  2.  * For legal stuff see the file COPYRIGHT
  3.  */
  4. #import "SessionEditor.h"
  5. #import "Controller.h"
  6.  
  7. @implementation SessionEditor
  8.  
  9. - awakeFromNib
  10. {
  11.   [descriptionField setScrollable:YES];
  12.   return self;
  13. }
  14.  
  15. /*
  16.  * Allow only one SessionEditor object to be created.
  17.  */
  18. + new
  19. {
  20.   static id inspector;
  21.  
  22.   if ( ! inspector ) {
  23.     inspector = [[SessionEditor alloc] init];
  24.     [NXApp loadNibSection:"SessionEditor.nib" owner:inspector withNames:NO];
  25.   }
  26.  
  27.   return inspector;
  28. }
  29.  
  30. - init
  31. {
  32.   [super init];
  33.   return self;
  34. }
  35.  
  36. - free
  37. {
  38.   return [super free];
  39. }
  40.  
  41. - (int)duration
  42. {
  43.   return [durationField intValue];
  44. }
  45.  
  46. - (const char *)startDateString
  47. {
  48.   return [startDateField stringValue];
  49. }
  50.  
  51. - (const char *)startTimeString
  52. {
  53.   return [startTimeField stringValue];
  54. }
  55.  
  56. - (const char *)description
  57. {
  58.   return [descriptionField stringValue];
  59. }
  60.  
  61. - (void)clearFields
  62. {
  63.   [durationField    setStringValue:""];
  64.   [startTimeField   setStringValue:""];
  65.   [startDateField   setStringValue:""];
  66.   [descriptionField setStringValue:""];
  67. }
  68.  
  69. /*
  70.  * Display the contents of a session object
  71.  */
  72. - (void)loadSession:(Session *)session
  73. {
  74.   if ( session == nil ) {
  75.     [self clearFields];
  76.     [startTimeField setStringValue:currentTime()];
  77.     [startDateField setStringValue:currentDate()];
  78.   } else {
  79.     [durationField setIntValue:[session minutes]];
  80.     [startTimeField setStringValue:[session startTimeString]];
  81.     [startDateField setStringValue:[session startDateString]];
  82.     [descriptionField setStringValue:[session description]];
  83.   }
  84. }
  85.  
  86. /*
  87.  * Copy the data from the form into the session object. If the
  88.  * object is nil, allocate one and return it.
  89.  */
  90. - (Session *)saveSession:(Session *)session
  91. {
  92.   const char *duration;
  93.   char *ptr;
  94.   int minutes;
  95.  
  96.   if ( session == nil )
  97.     session = [[Session alloc] init];
  98.   
  99.   duration = [durationField stringValue];
  100.   if ( (ptr = strchr( duration, ':' )) != NULL )
  101.     minutes = atoi( duration ) * 60 + atoi( ++ptr );
  102.   else if ( (ptr = strchr( duration, '.' )) != NULL )
  103.     minutes = atof( duration ) * 60;
  104.   else
  105.     minutes = atoi( duration );
  106.  
  107.   [session setMinutes:minutes];
  108.   [session setStartTimeString:[startTimeField stringValue]];
  109.   [session setStartDateString:[startDateField stringValue]];
  110.   [session setDescription:    [descriptionField stringValue]];
  111.  
  112.   return session;
  113. }
  114.  
  115. /*
  116.  * This routine should check that there is enough data to be useful...
  117.  */
  118. - (Session *)editItem:(Session *)session
  119. {
  120.   int value ;
  121.  
  122.   [self loadSession:session];
  123.   [form selectTextAt:0];    /* always select the first field */
  124.  
  125.   value = [NXApp runModalFor:panel] ;
  126.   [panel close];
  127.  
  128.   switch ( value  ) {
  129.   case NX_RUNSTOPPED:
  130.     return [self saveSession:session];    /* copy data back into struct */
  131.  
  132.   default:
  133.   case NX_RUNABORTED:
  134.     return nil;
  135.   }
  136. }
  137.  
  138. - cancel:sender
  139. {
  140.   [NXApp abortModal];
  141.   return self;
  142. }
  143.  
  144. - ok:sender
  145. {
  146.   int dummy;
  147.  
  148.   /* This is pretty cheesey. */
  149.   if ( sscanf([startDateField stringValue],
  150.           "%d/%d/%d", &dummy, &dummy, &dummy ) != 3 ||
  151.        sscanf([startTimeField stringValue],
  152.           "%d:%d", &dummy, &dummy ) != 2 ) {
  153.     NXRunAlertPanel( [NXApp appName], "Valid date (mm/dd/yy) and time (hh:mm) required.",
  154.             "It won't happen again", NULL, NULL );
  155.   } else
  156.     [NXApp stopModal];
  157.  
  158.   return self;
  159. }
  160.  
  161. @end
  162.  
  163.  
  164.